/**
 * 
 */
package com.easidea.tim.service.system.orgnization;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.system.orgnization.DepartmentDao;
import com.easidea.tim.entity.system.Geography;
import com.easidea.tim.entity.system.orgnization.Department;

/**
 * @author Harrison
 *
 */
public class DepartmentService extends EntityService<Department,DepartmentDao> {

	private DepartmentDao departmentDao;
	private static Logger log = Logger.getLogger(DepartmentService.class);
	
	
	public List<Department> getAllDepartmentList(){
		return this.departmentDao.getAllDepartmentList();
	}
	
	/**
	 * 
	 * 保存数据
	 * @param dept
	 * @return <code>0</code> 参数错误，空值
	 * 			<code>-2</code>保存修改的数据失败，无对应父对象
	 * 			<code>1</code>保存新的一级数据成功（不区分是一级还是二级）
	 * 			<code>2</code>保存修改的数据成功 
	 */
	public String saveDept(Department dept){
		if(dept != null){
			if(dept.getId() != null && dept.getId() != -1) {
				// update old dept data
				Department oldDept = departmentDao.get(dept.getId());
				if(oldDept != null){
					if(dept.getName() != null) oldDept.setName(dept.getName().trim());
					if(dept.getDeleted() != null)oldDept.setDeleted(dept.getDeleted());
					if(dept.getModifier() != null)oldDept.setModifier(dept.getModifier().trim());
					if(dept.getModifyTime() != null)oldDept.setModifyTime(dept.getModifyTime());
					if(dept.getParentDept() != null)oldDept.setParentDept(dept.getParentDept());
					if(dept.getArea()!= null && dept.getArea().getId() != null){
						Geography newArea = new Geography();
						newArea.setId(dept.getArea().getId());
						oldDept.setArea(newArea);
					}
					departmentDao.save(oldDept);
					log.debug("update department : " + dept.getName() + ", has parent ? " + dept.getParentDept() == null ? true:false);
					return "2";
				}else{
					log.debug("update department : " + dept.getName() + " failure ~~! caused by not found the data with id: "+dept.getId());
					return "-2";
				}
			}else{
				// add new dept data
				dept.setId(null); // fit for flex remote object conversions with int default value 0
				departmentDao.save(dept);
				log.debug("save department : " + dept.getName() + ", new Id : " + dept.getId());
				return (dept.getParentDept() != null ? "11:" : "1:")+dept.getId();
			}
		}else{
			log.error("save department error with null object !");
			return "0";
		}
	}
	
	/**
	 * 删除部门信息
	 * @param dept
	 * @return <code>0</code> 成功 <code>-1</code>空值 <code>-2</code>指定ID找不到数据 <code>-3</code>有子对象
	 */
	public short delDept(Department dept) {
		if(dept != null && dept.getId() != null) {
			Department oldDept = departmentDao.get(dept.getId());
			if(oldDept != null){
				if(oldDept.getSonList() != null && oldDept.getSonList().size() > 0){
					log.debug("delete department with id: "+oldDept.getId()+" and name : "+ oldDept.getName()+" failure, caused by it has son lists ~!");
					return -3; // 有子对象
				}else{
					short s = 1;
					oldDept.setDeleted(s);
					if(dept.getModifier() != null)oldDept.setModifier(dept.getModifier());
					oldDept.setModifyTime(new Date());
					log.debug("delete department with id: "+oldDept.getId()+" and name : "+ oldDept.getName()+" success ~!");
					return 0;
				}
			}else{
				log.debug("delete department with id: "+oldDept.getId()+" failure, caused by the data with assigned id does not exists ~!");
				return -2; // 无对应数据
			}
		}else{
			log.debug("can not delete null object with Department ~!");
			return -1;
		}
	}
	
	
	/* (non-Javadoc)
	 * @see org.springside.core.service.EntityService#getEntityDao()
	 */
	@Override
	public DepartmentDao getEntityDao() {
		return departmentDao;
	}



	/**
	 * @return the departmentDao
	 */
	public DepartmentDao getDepartmentDao() {
		return departmentDao;
	}



	/**
	 * @param departmentDao the departmentDao to set
	 */
	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}


}
